<template>
  <div class="content">
    <h2>登录</h2>
    <el-form
      ref="form"
      :model="loginForm"
      :rules="loginFormRules"
      label-width="80px"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="loginForm.username" placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="loginForm.password"
          placeholder="请输入密码"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref } from "vue";
import AsyncValidator from "async-validator";
import { useRouter } from "vue-router";
import { ElMessage } from 'element-plus'

const router = useRouter();
const form = ref(null);
const loginForm = ref({
  username: "",
  password: "",
});

const loginFormRules = {
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 3, max: 10, message: "用户名长度在 3 到 10 个字符", trigger: "blur" }
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 15, message: "密码长度在 6 到 15 个字符", trigger: "blur" }
  ]
};

const login = () => {  
  form.value.validate((valid) => {  
    if (valid) {  
      console.log("登录成功");  
      router.push('/index');  
    } else {  
      // 这里可以处理验证失败的情况，但 Element Plus 会自动显示错误信息  
      console.log('登录失败，有验证错误');
    }  
  });  
}; 
</script>

<style scoped>
.content {
  width: 400px;
  margin: 0 auto;
  border: 2px solid #4caf50; 
  border-radius: 10px; 
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); 
  background-color: #f0f0f0; 
  padding: 20px; 
}

h2 {
  color: #4caf50; 
  text-align: center; 
  margin-bottom: 30px; 
}

.el-form-item {
  margin-bottom: 20px; 
}

.el-button {
  padding: 10px 20px; 
  background-color: #4caf50; 
  border: none;
}
</style>